<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="前负责人" prop="formerHead" label-width="75px;">
        <el-input
          v-model="queryParams.formerHead"
          placeholder="请输入前负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前所属部门" prop="formerDept" label-width="85px">
        <el-input
          v-model="queryParams.formerDept"
          placeholder="请输入前所属部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际跟进记录" prop="realityFollowRecords">
        <el-date-picker clearable
          v-model="queryParams.realityFollowRecords"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际跟进记录">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最新跟进记录" prop="latestFollowRecords">
        <el-input
          v-model="queryParams.latestFollowRecords"
          placeholder="请输入最新跟进记录"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:customerHighSeas:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleRob"
          v-hasPermi="['crm:customerHighSeas:rob']"
        >抢客户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:customerHighSeas:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:customerHighSeas:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerHighSeasList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="客户名称" align="left" prop="name" width="80" :show-overflow-tooltip="true">
        <template slot-scope="scope">
              <a target="_blank" style="color:#007bff;" v-if="scope.row.id!=null"
                @click="detailReport(scope.row.customerId)">
                  {{ scope.row.name }}
                </a>
            <span v-else>111 {{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="left" prop="companyName" width="220px"/>
      <el-table-column label="客户类型" align="center" prop="type" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="前负责人" align="center" prop="formerHead" />
      <el-table-column label="前所属部门" align="center" prop="formerDept" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="实际跟进记录" align="center" prop="realityFollowRecords" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realityFollowRecords, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最新跟进记录" align="center" prop="latestFollowRecords" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRob(scope.row)"
            v-hasPermi="['crm:customerHighSeas:rob']"
          >抢客户</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:customerHighSeas:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改CRM客户公海对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="前负责人" prop="formerHead">
          <el-input v-model="form.formerHead" placeholder="请输入前负责人" />
        </el-form-item>
        <el-form-item label="前所属部门" prop="formerDept">
          <el-input v-model="form.formerDept" placeholder="请输入前所属部门" />
        </el-form-item>
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="实际跟进记录" prop="realityFollowRecords">
          <el-date-picker clearable
            v-model="form.realityFollowRecords"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择实际跟进记录">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最新跟进记录" prop="latestFollowRecords">
          <el-input v-model="form.latestFollowRecords" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerHighSeas, getCustomerHighSeas, delCustomerHighSeas, addCustomerHighSeas, updateCustomerHighSeas, customerHighSeasToCustomer } from "@/api/crm/customerHighSeas";

export default {
  name: "CustomerHighSeas",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // CRM客户公海表格数据
      customerHighSeasList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        formerHead: undefined,
        formerDept: undefined,
        name: undefined,
        companyName: undefined,
        type: undefined,
        phone: undefined,
        contacts: undefined,
        realityFollowRecords: undefined,
        latestFollowRecords: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键id不能为空", trigger: "blur" }
        ],
        formerHead: [
          { required: true, message: "前负责人不能为空", trigger: "blur" }
        ],
        formerDept: [
          { required: true, message: "前所属部门不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "客户类型不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        realityFollowRecords: [
          { required: true, message: "实际跟进记录不能为空", trigger: "blur" }
        ],
        latestFollowRecords: [
          { required: true, message: "最新跟进记录不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询CRM客户公海列表 */
    getList() {
      this.loading = true;
      listCustomerHighSeas(this.queryParams).then(response => {
        this.customerHighSeasList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    detailReport(customerId) {
      console.log(customerId);
      this.$router.push({ path: '/crm/customerdetail/index', query: { customerId: customerId, type:"customerhighseas" } })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        formerHead: undefined,
        formerDept: undefined,
        name: undefined,
        companyName: undefined,
        type: undefined,
        phone: undefined,
        contacts: undefined,
        realityFollowRecords: undefined,
        createBy: undefined,
        createTime: undefined,
        latestFollowRecords: undefined,
        remark: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加CRM客户公海";
    },
    /** 转客户按钮操作 */
    handleRob(row) {
      this.loading = true;
      const id = row.id || this.ids
      let data = '{"ids":"' + id + '"}';
      customerHighSeasToCustomer(data).then(response => {
        console.info(response);
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCustomerHighSeas(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCustomerHighSeas(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除CRM客户公海编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCustomerHighSeas(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/customerHighSeas/export', {
        ...this.queryParams
      }, `customerHighSeas_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
